import random
from datetime import datetime

money = 10000
goods = [
    {'name': '冰箱', 'price': 2000},
    {'name': '可乐', 'price': 5},
    {'name': '空调', 'price': 1500},
    {'name': '雪碧', 'price': 6},
    {'name': '老坛酸菜', 'price': 4}
]


def discount():  # 打折
    names = [good['name'] for good in goods]
    discount_item = random.choice(names)
    print(f'您的打折商品是: {discount_item}')
    return discount_item


def timea():  # 购物时间
    now = datetime.now()
    custom_format = now.strftime("%Y.%m.%d %H:%M:%S")
    return custom_format


# 初始化购物车（使用字典存储商品名称和购买信息）
cart = {}
discount_item = discount()  # 获取打折商品
fund = False

while True:
    shopping = input("请输入你购买的商品：")
    fund = False

    # 查找商品
    for good in goods:
        if shopping == good['name']:
            fund = True
            # 如果是打折商品，计算打折后的价格
            discounted_price = int(good['price'] * 0.8) if shopping == discount_item else good['price']

            if money >= discounted_price:
                if shopping in cart:
                    cart[shopping]['quantity'] += 1
                else:
                    cart[shopping] = {
                        'price': discounted_price,
                        'quantity': 1
                    }

                money -= discounted_price
                print(f'买的{shopping},余额为：{money}')

                # 如果是打折商品，提示用户
                if shopping == discount_item:
                    print(f"恭喜！您购买的 {shopping} 是打折商品！")
            else:
                print('穷鬼，钱不够！请到其他超市购买！')
            break

    if not fund:
        print("没有这个商品，别瞎弄！")

    a = input("是否继续购物?是否退货？退出购物请输入Q/q:\t退货请输入r/R:")
    if a.lower() == "q":
        break
    if a.lower() == "r":
        return_goods=input("请输入退货商品的名称：")
        if return_goods in cart:
            sum_return_goods=int(input('请输入退货数量：'))
            for item,info in cart.items():
                if return_goods ==item:
                    if sum_return_goods <=info['quantity']:
                        info['quantity'] -= sum_return_goods
                        print(f'您已退{return_goods},数量为{sum_return_goods}')
                    else:
                        print('输入数量有误！')
                    break
        else:
            print('您未购买该商品。')
print("\n--------------购物小票--------------")
print('商品', end='\t\t')
print('价格', end='\t\t')
print('数量', end='\t\t')
print()
b=0
for item, info in cart.items():
    a=info['price']*info['quantity']
    b+=a
    print(item, end='\t\t')
    print(info['price'], end='\t\t')
    print(info['quantity'], end='\t\t')
    print()

print("-----------------------------------")
print(f'您的余额是：{money}')
print('购物时间为：', timea())
print('总价为:',b)